package TakeAwaySystem.businessoperation.food;

import TakeAwaySystem.Business;
import TakeAwaySystem.businessoperation.BusinessIOperatition;
import TakeAwaySystem.Food;
import TakeAwaySystem.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author Vv
 * @version 1.0
 * @project coding_learn
 * @description
 * @date 2023/9/26 22:26:58
 */
public class BusinessDelectFoodOperation implements BusinessIOperatition {
    @Override
    public void work(Business o, File file) {
        List<Food> foodList = Food.fileToList(Food.file);
        List<Food> businessFood = new ArrayList<>();
        for(Food food : foodList){
            if(food.getBusinessId() == o.getId()){
                businessFood.add(food);
            }
        }
        int N = businessFood.size();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择你要删除的哪个商品");
        String str = null;
        for(int i = 0;i<N;i++){
            System.out.println((i+ 1) +" . "+businessFood.get(i).Name);
        }
        int choice = -1;
        System.out.println("你如果确定要删除商品请输入 YES否则输入其他");
        str = scanner.next();
        while(str.equals("YES")){
            System.out.println("请输入你要删除的商品");
            choice = scanner.nextInt();
            System.out.println("你要删除的是 ： " + businessFood.get(choice - 1).getName());
            System.out.println("如果确定要删除请在输入YES");
            str = "";
            str = scanner.next();
            if(str.equals("YES")){
                foodList.remove(businessFood.get(choice - 1));
                System.out.println("删除成功");
            }
            str = "";
            System.out.println("如果还想进行删除操作 请在键盘输入YES");
            str = scanner.next();
        }
        System.out.println("结束删除操作");
        Food.ListToFoodFile(foodList,Food.file);
    }

    @Override
    public void ListToFile(List<User> list,File file) {
        // File file = new File("d:/Business.txt");
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
        try {
            fileWriter = new FileWriter(file);
            bw = new BufferedWriter(fileWriter);
            int N = list.size();
            for(int i = 0;i<N;i++){
                Business user = (Business) list.get(i);
                StringBuilder sbr = new StringBuilder();
                sbr.append(user.getId() + " ");
                sbr.append(user.getPassword() + " ");
                sbr.append(user.getName() + " ");
                sbr.append(user.getBusinessAddress() + " ");
                sbr.append(user.getBusinessExplain() + " ");
                sbr.append(user.getStarPrice() + " ");
                sbr.append(user.getDeliveryPrice());
                bw.write(sbr.toString());
                bw.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                bw.close();
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }

    }


    @Override
    public List<User> fileToList(File file)  {
        List<User> list = new ArrayList<>();
        // File file = new File("d:/Business.txt");
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            while(true){
                String str = br.readLine();
                if(str != null){
                    String[] arr = str.split(" ");
                    Business users = new Business();
                    users.setId(Integer.valueOf(arr[0]));
                    users.setPassword(arr[1]);
                    users.setName(arr[2]);
                    users.setBusinessAddress(arr[3]);
                    users.setBusinessExplain(arr[4]);
                    users.setStarPrice(Double.valueOf(arr[5]));
                    users.setDeliveryPrice(Double.valueOf(arr[6]));
                    list.add(users);
                }else{
                    break;
                }
            }
        }catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fr.close();
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return list;
    }
}
